'''
Created on Feb 10, 2015

@author: eyaomai
'''
import sys
sys.path.append(sys.argv[0][:sys.argv[0].rfind('com/stocklens')])
import time
import unittest
from com.stocklens.stock.common.utils import Logging, CTPUtils
from com.stocklens.stock.data.miit import ImportantNewsCrawler,MIITManager
class TestImportantNews(unittest.TestCase):
    '''
    classdocs
    '''


    def setUp(self):
        '''
        Constructor
        '''
        self.infoList= list()
        self.isInit = True
        self.firstOfEach = True
        self.expectedInfo = list()
        self.expectedNum = 0
    def randomSleep(self):
        time.sleep(1)
    def sendMail(self, prefix, infoList):
        Logging.LOGGER.debug('len of infoList:%d', len(infoList))
        if self.isInit:
            self.infoList = infoList
        else:
            
            if self.firstOfEach is True:
                self.assertEqual(len(infoList), self.expectedNum, 'len is not expected:%d!=%d' % (len(self.expectedInfo), self.expectedNum))
                self.firstOfEach = False
            else:
                Logging.LOGGER.error('unexpected infolist received:%s', infoList)
    def testRepeat(self):
        request = {MIITManager.PARA_SEND_MAIL: self.sendMail,
                   ImportantNewsCrawler.PARA_URL:'http://www.miit.gov.cn/n11293472/n11293832/n11293907/n11368223/index.html',
                   ImportantNewsCrawler.PARA_PREFIX:'workingtrend',
                   'failureTableName':'failureTableName'}
        crawler = ImportantNewsCrawler(self, None, request)
        crawler._ImportantNewsCrawler__lastTitle = 'invalid'
        crawler.start()
        time.sleep(3)
        self.assertEqual(len(self.infoList), 20, 'infolist len is not expected %d' % len(self.infoList))
        
        self.expectedNum=2
        self.firstOfEach=True
        crawler._ImportantNewsCrawler__lastTitle = self.infoList[self.expectedNum].brief
        time.sleep(2)

        self.expectedNum=1
        self.firstOfEach=True
        crawler._ImportantNewsCrawler__lastTitle = self.infoList[self.expectedNum].brief
        time.sleep(10)


        #self.assertEqual(len(self.expectedInfo), self.expectedNum, 'len is not expected:%d!=%d' % (len(self.expectedInfo), self.expectedNum))
        crawler.shutDown()
        
if __name__ == '__main__':
    logfile = 'conf/ut/ut.logging.cfg'
    Logging.initLogger(logfile)
    unittest.main()